/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package ar.com.siglo21.ppo.view;
import ar.com.siglo21.ppo.controller.ControladorEjemplar;
import ar.com.siglo21.ppo.controller.ControladorPersonal;
import ar.com.siglo21.ppo.controller.ControladorPrestamo;
import ar.com.siglo21.ppo.controller.ControladorSocio;

import ar.com.siglo21.ppo.model.Ejemplar;
import ar.com.siglo21.ppo.model.EstadoPrestamo;
import ar.com.siglo21.ppo.model.Personal;
import ar.com.siglo21.ppo.model.Prestamo;
import ar.com.siglo21.ppo.model.Socio;
import ar.com.siglo21.ppo.util.TrabajoIntegradorException;
import java.sql.SQLException;
import java.util.Iterator;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;

public class DatosPrestamoView extends javax.swing.JFrame {
    
    private Prestamo prestamo;
    private ControladorPrestamo controladorPrestamo;
    private ControladorSocio controladorSocio;
    private ControladorEjemplar controladorEjemplar;
    private Parametros parametros;

    /**
     * Creates new form DatosAutorView
     */
    public DatosPrestamoView() {
        initComponents();
    }
    
    public DatosPrestamoView(Parametros parametros) {
        controladorPrestamo = new ControladorPrestamo();
        controladorEjemplar = new ControladorEjemplar();
        controladorSocio = new ControladorSocio();
        initComponents();
        this.setTitle("Nuevo Prestamo");
        this.prestamo = new Prestamo();
        this.parametros = parametros;
        cargarEjemplares();
        cargarSocios();
    }
    
    public DatosPrestamoView(Prestamo prestamo, Parametros parametros) {
        
         controladorPrestamo = new ControladorPrestamo();
        controladorEjemplar = new ControladorEjemplar();
        controladorSocio = new ControladorSocio();
        
        initComponents();
        this.setTitle("Prestamo Genero");
        this.prestamo = prestamo;
        this.parametros = parametros;
        cargarEjemplares();
        cargarSocios();
        
//        jTextFieldNombre.setText(personal.getNombre());
//        jTextFieldApellido.setText(personal.getApellido());
//        jTextFieldDni.setText(String.valueOf(personal.getDni()));
//        jTextFieldEmail.setText(personal.getEmail());
//        jTextFieldUsuario.setText(personal.getUsuario());
//        jTextFieldPassword.setText(personal.getPassword());
//        jTextFieldRol.setText(personal.getRol());
        
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        jButtonGuardarPersonal = new javax.swing.JButton();
        jLabel3 = new javax.swing.JLabel();
        jComboBoxEjemplar = new javax.swing.JComboBox();
        jLabel9 = new javax.swing.JLabel();
        jComboBoxSocio = new javax.swing.JComboBox();
        jLabel2 = new javax.swing.JLabel();
        jTextFieldFecha = new javax.swing.JTextField();
        jLabel4 = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);

        jLabel1.setText("Ejemplar");

        jButtonGuardarPersonal.setText("Guardar");
        jButtonGuardarPersonal.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonGuardarPersonalActionPerformed(evt);
            }
        });

        jLabel3.setText("PRESTAMOS");

        jLabel9.setText("Socio");

        jLabel2.setText("Fecha");

        jTextFieldFecha.setText("jTextField1");
        jTextFieldFecha.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jTextFieldFechaActionPerformed(evt);
            }
        });

        jLabel4.setText("(dd-MM-YYYY)");

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGap(218, 218, 218)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jButtonGuardarPersonal)
                            .addComponent(jLabel3)))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGap(25, 25, 25)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel9)
                            .addComponent(jLabel1)
                            .addComponent(jLabel2))
                        .addGap(24, 24, 24)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jComboBoxEjemplar, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addGroup(jPanel1Layout.createSequentialGroup()
                                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(jComboBoxSocio, javax.swing.GroupLayout.PREFERRED_SIZE, 227, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addGroup(jPanel1Layout.createSequentialGroup()
                                        .addComponent(jTextFieldFecha, javax.swing.GroupLayout.PREFERRED_SIZE, 149, javax.swing.GroupLayout.PREFERRED_SIZE)
                                        .addGap(18, 18, 18)
                                        .addComponent(jLabel4)))
                                .addGap(0, 234, Short.MAX_VALUE)))))
                .addContainerGap())
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel3)
                .addGap(19, 19, 19)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(jComboBoxEjemplar, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel9)
                    .addComponent(jComboBoxSocio, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(23, 23, 23)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(jTextFieldFecha, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel4))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 112, Short.MAX_VALUE)
                .addComponent(jButtonGuardarPersonal)
                .addGap(20, 20, 20))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel1, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jButtonGuardarPersonalActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonGuardarPersonalActionPerformed
        
          Socio socio = (Socio) jComboBoxSocio.getSelectedItem();
          Ejemplar ejemplar = (Ejemplar) jComboBoxEjemplar.getSelectedItem();
          prestamo.setFechaInicio(jTextFieldFecha.getText());
          prestamo.setEjemplar(ejemplar);
          EstadoPrestamo estadoPrestamo = new EstadoPrestamo();
          estadoPrestamo.setDescripcion("VIGENTE");
          estadoPrestamo.setId(1);
          prestamo.setEstadoPrestamo(estadoPrestamo);
          prestamo.setSocio(socio);
         
         


        try{
            if(prestamo.getId()==0){

                controladorPrestamo.agregar(prestamo);
                this.dispose();
                parametros.datosActualizados();
            }else{
                controladorPrestamo.actualizar(prestamo);
                this.dispose();
                parametros.datosActualizados();
            }
        }catch(SQLException | TrabajoIntegradorException exception){
            System.out.println(exception.getMessage());
        }

    }//GEN-LAST:event_jButtonGuardarPersonalActionPerformed

    private void jTextFieldFechaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jTextFieldFechaActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_jTextFieldFechaActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(DatosPrestamoView.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(DatosPrestamoView.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(DatosPrestamoView.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(DatosPrestamoView.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>
        //</editor-fold>
        //</editor-fold>
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new DatosPrestamoView().setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButtonGuardarPersonal;
    private javax.swing.JComboBox jComboBoxEjemplar;
    private javax.swing.JComboBox jComboBoxSocio;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel9;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JTextField jTextFieldFecha;
    // End of variables declaration//GEN-END:variables

    private void cargarEjemplares() {
        try {
            List<Ejemplar> listaEjemplarDisponible = controladorEjemplar.cargarEjemplaresDisponibles();
            for (Iterator<Ejemplar> iterator = listaEjemplarDisponible.iterator(); iterator.hasNext();) {
                Ejemplar next = iterator.next();
                jComboBoxEjemplar.addItem(next);
            }

        } catch (SQLException | TrabajoIntegradorException ex) {
            Logger.getLogger(DatosLibroView.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    private void cargarSocios() {
        try {
            List<Socio> listaSocios = controladorSocio.cargarSocio();
            for (Iterator<Socio> iterator = listaSocios.iterator(); iterator.hasNext();) {
                Socio next = iterator.next();
                jComboBoxSocio.addItem(next);
            }

        } catch (SQLException | TrabajoIntegradorException ex) {
            Logger.getLogger(DatosLibroView.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
}
